//
//  TSAccountInfoView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/6.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON

struct TSAccountUpgradeModel : HandyJSON {
    var bai : CGFloat = 0       // 完成的百分百
    var cha : Int = 0           // 还差多少升级
    var liushui : Int = 0       // 目前流水
}

class TSAccountInfoView: UIView {

    var bgView : UIView!
    var avatar : UIImageView!
    var nameLabel : UILabel!
    var identifyLabel : UILabel!
    var progressView : TSProgressView!
    var upgradeLabel : UILabel!
    var upgradeModel : TSAccountUpgradeModel? = nil
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupAccountInfoView() {
        
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂无记录"
        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        upgradeLabel.attributedText = createAttributedStr(str: "距离升级还需要0流水", attributedStr: "0")
    }
    
    func updateProgress(model:TSAccountUpgradeModel) {
        
        self.upgradeModel = model
        
        upgradeLabel.attributedText = createAttributedStr(str: String.init(format: "距离升级还需要%d流水", model.cha), attributedStr: "\(model.cha)")
        
        let level = TSLoginManager.shared._usermodel?.dengji ?? 0
        progressView.setupProgressView(value:model.bai, start: "LV\(level)", end: "LV\(level+1)")
    }
    
//    func updateHeaderViewFrame(offsetY:CGFloat) {
//        bgView.snp.updateConstraints { (make) in
//            make.height.equalTo(168+STATUSBAR_HEIGHT+offsetY)
//        }
//    }
    
    @objc func cardAction() {
        let currentVC = UIViewController.currentViewController()
        currentVC.navigationController?.pushViewController(TSCardListViewController(), animated: true)
    }
    
}

// MARK: - Initialize
extension TSAccountInfoView {
    
    func configViews() {
        
        bgView = UIView.init()
        bgView.backgroundColor = BASE_PURPLE_COLOR
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(168+STATUSBAR_HEIGHT)
        }
        
        let infoView = UIView.init()
        infoView.backgroundColor = WHITE_COLOR
        infoView.layer.cornerRadius = 10
        infoView.layer.masksToBounds = true
        self.addSubview(infoView)
        infoView.snp.makeConstraints { (make) in
            make.centerX.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*12, height: 139))
        }
        
        avatar = UIImageView.init()
        avatar.image = UIImage(named: "general_avatar_default")
        avatar.backgroundColor = BASE_PURPLE_COLOR
        avatar.layer.borderWidth = 1
        avatar.layer.borderColor = WHITE_COLOR.cgColor
        avatar.layer.cornerRadius = 30
        avatar.layer.masksToBounds = true
        self.addSubview(avatar)
        avatar.snp.makeConstraints { (make) in
            make.left.equalTo(44)
            make.bottom.equalTo(-109)
            make.size.equalTo(CGSize(width: 60, height: 60))
        }
        
        let cardBtn = UIButton.init(type: .custom)
        cardBtn.setBackgroundImage(UIImage(named: "account_info_bank"), for: .normal)
        cardBtn.addTarget(self, action: #selector(cardAction), for: .touchUpInside)
        infoView.addSubview(cardBtn)
        cardBtn.snp.makeConstraints { (make) in
            make.top.equalTo(avatar.snp_bottom).offset(5)
            make.right.equalToSuperview()
            make.size.equalTo(CGSize(width: 60, height: 22))
        }
        
        nameLabel = UILabel.init()
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂无记录"
        nameLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#292929")
        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        infoView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar)
            make.right.equalTo(cardBtn.snp_left).offset(-10)
            make.top.equalTo(avatar.snp_bottom).offset(10)
        }
        
        identifyLabel = UILabel.init()
        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        identifyLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#292929")
        identifyLabel.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        infoView.addSubview(identifyLabel)
        identifyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.right.equalToSuperview().inset(10)
            make.top.equalTo(nameLabel.snp_bottom).offset(10)
        }
        
        progressView = TSProgressView.init()
        progressView.backgroundColor = WHITE_COLOR
        infoView.addSubview(progressView)
        progressView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(identifyLabel.snp_bottom).offset(15)
            make.height.equalTo(8)
        }
        
        upgradeLabel = UILabel.init()
        upgradeLabel.attributedText = createAttributedStr(str: "距离升级还需要0流水", attributedStr: "0")
        upgradeLabel.textAlignment = .center
        infoView.addSubview(upgradeLabel)
        upgradeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(39)
            make.right.equalToSuperview().inset(39)
            make.top.equalTo(progressView.snp_bottom).offset(5)
        }
    
    }
    
    func createAttributedStr(str:String, attributedStr:String) -> NSAttributedString {
        let text = NSString(string: str)
        let attribute = NSMutableAttributedString.init(string: str)
        
        attribute.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#A5ABB7"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10)], range: NSMakeRange(0, str.count))
        
        attribute.addAttributes([NSAttributedString.Key.foregroundColor:BASE_PURPLE_COLOR], range: text.range(of: attributedStr))
        return attribute.copy() as! NSAttributedString
    }
}
